import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
      
import axios from 'axios';

import App from './App.vue';
import store from './store';
import router from './router/index.js';

import echarts from 'echarts';
import VueI18n from 'vue-i18n';
import { formatSeconds } from './utils/tools.js';
import 'babel-polyfill';
import './assets/css/main.css';
import './assets/css/theme.scss';
import './assets/css/publicStyle.less'

// 视频播放
import VideoPlayer from 'vue-video-player/src';
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'
window.videojs = VideoPlayer.videojs
require('video.js/dist/lang/zh-CN.js')
Vue.use(VideoPlayer)

// 导入Element-UI 语言包
import elementZH from 'element-ui/lib/locale/lang/zh-CN'// 中文
import elementRU from 'element-ui/lib/locale/lang/ru-RU'// 俄文
import elementKz from 'element-ui/lib/locale/lang/kz'// 哈文
import elementEn from 'element-ui/lib/locale/lang/en'// 英文
// 导入自己自定义 语言包
import Chinese from "./components/lang/zh"; // 中文
import Russian from "./components/lang/ru"; // 俄文
import Kazakhstan from "./components/lang/kz"; // 哈文
import English from "./components/lang/en"; // 英文

window.eventBus = new Vue()
Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
});
Vue.use(VueI18n);
// const i18n = new VueI18n({
//   locale: 'en',
//   messages
// });
const i18n = new VueI18n({
  locale: !!JSON.parse(localStorage.getItem('language'))?JSON.parse(localStorage.getItem('language')).lang:'zh',
  messages:{
    zh: {
      ...elementZH, // 将饿了么的中文语言包引入
      ...Chinese, // 将自己自定义的中文语言包引入
    },
    ru: {
      ...elementRU, // 将饿了么的俄文语言包引入
      ...Russian, // 将自己自定义的俄文语言包引入
    },
    kz:{
      ...elementKz, // 将饿了么的哈文语言包引入
      ...Kazakhstan, // 将自己自定义的哈文语言包引入
    },
    en:{
      ...elementEn, // 将饿了么的英文语言包引入
      ...English, // 将自己自定义的英文语言包引入
    }
  },
});
Vue.use(VXETable)
Vue.prototype.$axios = axios;
Vue.prototype.$echarts = echarts;
Vue.prototype.$formatSeconds = formatSeconds; // 全局使用该工具函数

import VueBarcode from'@xkeshi/vue-barcode';//导入条形码插件
Vue.component('barcode',VueBarcode); //声明条形码组件
import htmlToPdf from '@/utils/htmlToPdf'
Vue.use(htmlToPdf)

//input聚焦
Vue.directive('fo', {
  inserted (el, binding, vnode) {
    // 聚焦元素
    el.querySelector('input').focus()
    window.setTimeout(() => {
      el.querySelector('input').select()
    }, 10);
  }
})
Vue.directive('fo-select', {
  inserted (el, binding, vnode) {
    // 聚焦元素
    vnode.componentInstance.toggleMenu()
  }
})
Vue.directive('noFocu', {
  inserted (el, binding, vnode) {
    // 获取input number组件的加减按钮
    const buttons = el.querySelectorAll('.el-input-number__increase, .el-input-number__decrease');
    buttons.forEach(button => {
      button.addEventListener('mousedown', (event) => {
        // 阻止默认行为，防止输入框失去焦点
        event.preventDefault();
      });
    });
  }
})
Vue.directive('number-only', {
  bind(el) {
    el.addEventListener('input', (e) => {
      const value = e.target.value;
      e.target.value = value.replace(/[^\d.]/g, '')
        .replace(/^\./g, '')
        .replace(/\.{2,}/g, '.')
        .replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
    });
  }
});
// 定义baseUrl全局变量
Vue.prototype.$baseUrl = process.env.VUE_APP_BASE_URL
Vue.prototype.$baseUrlKz = process.env.VUE_APP_BASE_URL_KZ
// 定义标签打印全局变量
import {getLodop} from '@/utils/Lodop/LodopFuncs.js'
Vue.prototype.getLodop = getLodop

// 引入封装自己封装的组件
import FilterBox from '@/components/common/FilterBox';
Vue.component('FilterBox', FilterBox);
import MyTable from '@/components/common/MyTable';
Vue.component('MyTable', MyTable);
import Pagination from '@/components/common/Pagination';
Vue.component('Pagination', Pagination);
// 多个page使用
import PaginationRight from '@/components/common/Pagination_right';
Vue.component('PaginationRight', PaginationRight);
// 不分页显示全部的底部
import DialogPagination from '@/components/common/dialog_Pagination';
Vue.component('DialogPagination', DialogPagination);
import Multiselect from '@/components/common/multi_select';
Vue.component('Multiselect', Multiselect);
import Loadselect from '@/components/common/load_select';
Vue.component('Loadselect', Loadselect);
import PasswordInput from '@/components/common/password_input';
Vue.component('PasswordInput', PasswordInput);


// 全局挂载拦截器
import {computing} from '@/utils/func'
import {post,get,postForm,downloadCenterLoad} from '@/utils/HttpRequest'

// 定义全局变量
Vue.prototype.$computing = computing
Vue.prototype.$post = post
Vue.prototype.$get = get
Vue.prototype.$postForm = postForm
Vue.prototype.$downloadCenterLoad = downloadCenterLoad

Array.prototype.pushNoRepeat = function(){ // 往数组里添加不重复数据
  for(var i=0; i<arguments.length; i++){
    var ele = arguments[i];
    if(this.indexOf(ele) == -1){
      this.push(ele);
    }
  }
}


Vue.config.productionTip = false;


// 使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  let lang=!!JSON.parse(localStorage.getItem('language'))?JSON.parse(localStorage.getItem('language')).lang:'zh' 
  const token = localStorage.getItem('token');
  if (!token && to.path !== '/Login' && to.path !== '/ConsignmentNotePrint') {
    if(lang=='zh'){
      ElementUI.Notification({
        title: '未登录提醒',
        message: '请登陆后访问！',
        type: 'warning',
        duration:2000
      });
    }else if(lang=='ru'){
      ElementUI.Notification({
        title: 'Напоминание о том, что Вы не вошли в систему',
        message: 'Пожалуйста, войдите в систему, чтобы посетить!',
        type: 'warning',
        duration:2000
      });
    }else if(lang=='kz'){
      ElementUI.Notification({
        title: 'Еске салғыш жүйеге кірмеген',
        message: 'Келу үшін жүйеге кіріңіз!',
        type: 'warning',
        duration:2000
      });
    }else if(lang=='en'){
      ElementUI.Notification({
        title: 'Not logged in reminder',
        message: 'Please log in and visit!',
        type: 'warning',
        duration:2000
      });
    }
    window.setTimeout(()=>{
      next('/Login');
    },2000)
  } else {
    // window.setTimeout(()=>{
    //   let menuGroupArr=store.state.menuGroup.filter(item=>{return item.route==to.name})
    //   let isInmenuGroupArr=menuGroupArr.length>0  //是否属于菜单路由
    //   let menuId=0
    //   if(isInmenuGroupArr){
    //     menuId=menuGroupArr[0].m_id
    //   }
    //   let PermissionGroup=''
    //   if(!!JSON.parse(sessionStorage.getItem('userData'))){
    //     PermissionGroup=JSON.parse(sessionStorage.getItem('userData')).jurisdiction.split(',')
    //   }
    //   if(!!PermissionGroup){
    //     let isPermission=!!menuId&&PermissionGroup.findIndex(item=>{return item==menuId})==-1 //是否有权限访问菜单
    //     if(isInmenuGroupArr&&isPermission){
    //       next('/403');
    //     }else{
    //       next();
    //     }

    //   }else{
        next();
    //   }
    // },100)
  }
});
router.onError((error) => {
  alert(error)
})

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
});